<template>
  <div class="main">
    <div class="container">
      <div class="container-top">
        <div class="top">
          <div class="title">申报用户管理</div>
        </div>
        <div class="search">
          <div>
            <el-checkbox v-model="checkAll" @change="handleCheckAllChange"
              >全选</el-checkbox
            >
            <div class="del">
              <el-button
                icon="el-icon-error"
                class="batchdelete"
                @click="queryUserReportDelBatch"
                >批量删除</el-button
              >
            </div>
            <router-link to="/create-user" class="create">
              <i class="iconfont icon-chuangjian"></i>
              创建新用户
            </router-link>
          </div>
          <el-form :inline="true" :model="formInline" class="demo-form-inline">
            <el-form-item class="number">
              <el-input
                v-model="formInline.number"
                placeholder="账号"
              ></el-input>
            </el-form-item>
            <el-form-item class="name">
              <el-input v-model="formInline.name" placeholder="姓名"></el-input>
            </el-form-item>
            <el-form-item class="number">
              <el-input
                v-model="formInline.tel"
                placeholder="联系电话"
              ></el-input>
            </el-form-item>
            <el-form-item class="state">
              <el-select v-model="formInline.status" placeholder="状态">
                <el-option label="全部" value="0"></el-option>
                <!-- <el-option label="待审核" value="1"></el-option> -->
                <el-option label="正常" value="2"></el-option>
                <el-option label="禁止" value="3"></el-option>
              </el-select>
            </el-form-item>
            <el-form-item class="number">
              <el-input
                v-model="formInline.yxName"
                placeholder="系/院"
              ></el-input>
            </el-form-item>
            <el-form-item class="number">
              <el-input
                v-model="formInline.bmName"
                placeholder="部门"
              ></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="onSearch">搜索</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
      <el-table
        ref="multipleTable"
        :data="tableData"
        tooltip-effect="dark"
        @selection-change="handleSelectionChange"
        style="width: 100%"
      >
        <el-table-column type="selection" width="80"> </el-table-column>
        <el-table-column prop="user_name" label="账号" width="140">
        </el-table-column>
        <el-table-column prop="yx_name" label="院系/部门"> </el-table-column>
        <el-table-column prop="real_name" label="姓名"> </el-table-column>
        <el-table-column prop="phone" label="联系电话" width="160">
        </el-table-column>
        <el-table-column prop="status_show" label="状态" width="150">
        </el-table-column>
        <el-table-column label="创建时间" width="200">
          <template slot-scope="props">
            <span>{{ props.row.add_time | formatTime }}</span>
          </template>
        </el-table-column>
        <el-table-column fixed="right" label="操作" width="200" current-row-key>
          <template slot-scope="scope">
            <el-button
              @click="handleModify(scope.row)"
              type="text"
              size="small"
              class="edit"
              >编辑</el-button
            >
            <el-button
              type="text"
              size="small"
              class="del"
              @click="queryUserReportDel(scope.row, scope.$index)"
              >删除</el-button
            >
            <el-button
              v-if="scope.row.status === '2'"
              type="text"
              size="small"
              class="disable"
              @click="onDisable(scope.row)"
              >禁用</el-button
            >
            <el-button
              v-if="scope.row.status === '3'"
              type="text"
              size="small"
              class="relieve"
              @click="onDisable(scope.row)"
              >解除</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <div class="block">
        <el-pagination
          background
          @current-change="handleCurrentChange"
          :current-page.sync="currentPage"
          :page-count="total_page"
          :page-size="page_size"
          layout="prev, pager, next,slot"
          :total="totalAll"
        >
          <el-input v-model="page"></el-input>
          <span @click="jumperPage()">GO</span>
        </el-pagination>
      </div>
    </div>
  </div>
</template>
<script>
import moment from "moment";
import {
  UserReport,
  UserReportDel,
  UserReportDelBatch,
  UserReportDisable,
} from "@/api/login";

export default {
  name: "UserManagement",
  data() {
    return {
      formInline: {
        number: "", // 账号
        name: "", // 姓名
        tel: "", // 联系方式
        status: "",
        yxName: "", //系院
        // bmName: "", //部门
      },
      // 当前页码
      currentPage: 1,
      // 输入页码
      page: 1,
      // 总页码数
      total_page: 0,
      // 每页展示个数
      page_size: 7,
      // 总条目数
      totalAll: 0,
      // 批量删除
      delArr: [],
      tableData: [],
      multipleSelection: [],
      value: null,
      checkAll: false,
      isIndeterminate: false,
      token: "",
      Showbg: false,
    };
  },
  filters: {
    formatTime(time) {
      return moment(time * 1000).format("YYYY-MM-DD HH:mm");
    },
  },
  methods: {
    onSearch() {
      this.queryUserReport();
    },
    // handleClick(row) {
    //   console.log(row);
    // },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.queryUserReport();
    },
    jumperPage() {
      if (this.page > this.total_page) {
        this.page = this.total_page;
        alert("超过总页数");
      }
      this.currentPage = this.page - 0;
      this.queryUserReport();
    },
    // 点击编辑
    handleModify(row) {
      this.$router.push({
        path: "/modify-user",
        query: {
          userid: row.user_id,
        },
      });
    },
    // 表格全选
    handleSelectionChange(val) {
      if (val.length <= this.tableData.length && val.length > 0) {
        this.isIndeterminate = true;
        this.delArr = [];
        val.forEach((item, index) => {
          const obj = {};
          obj["index"] = index;
          obj["user_id"] = item.user_id;
          this.delArr.push(obj);
        });
      } else if (val.length == this.tableData.length) {
        this.isIndeterminate = false;
        this.checkAll = true;
      } else if (val.length == 0) {
        this.isIndeterminate = false;
        this.checkAll = false;
        this.delArr = [];
      }
    },
    // 点击禁用/解除
    onDisable(row) {
      if (row.status === "2") {
        row.status = "3";
      } else {
        row.status = "2";
      }
      this.queryUserReportDisable(row);
    },
    // 删除用户
    queryUserReportDel(row, index) {
      UserReportDel({
        token: this.token,
        user_id: row.user_id,
      }).then((result) => {
        if (result.code === 1) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.tableData.splice(index, 1);
        }
      });
    },
    // 批量删除用户
    queryUserReportDelBatch() {
      // console.log("删除" + this.delArr);
      const delUserid = [];
      this.delArr.forEach((item) => {
        delUserid.push(item.user_id);
      });
      UserReportDelBatch({
        token: this.token,
        user_ids: delUserid.toString(),
      }).then((result) => {
        if (result.code === 1) {
          this.$message({
            message: "删除成功",
            type: "success",
          });
          this.delArr.forEach((item) => {
            this.tableData.forEach((el, i) => {
              if (item.user_id === el.user_id) {
                this.tableData.splice(i, 1);
              }
            });
          });
          this.queryUserReport();
        }
      });
    },
    // 禁用/解除用户
    queryUserReportDisable(row) {
      UserReportDisable({
        token: this.token,
        user_id: row.user_id,
        status: row.status,
      }).then((result) => {
        if (result.code === 1) {
          this.$message({
            message: "执行成功",
            type: "success",
          });
          var currstatus = row.status == "2" ? "正常" : "禁止";
          row.status_show = currstatus;
        }
      });
    },
    // 获取用户列表
    queryUserReport() {
      UserReport({
        token: this.token,
        user_name: this.formInline.number,
        real_name: this.formInline.name,
        phone: this.formInline.tel,
        status: this.formInline.status,
        yx_name: this.formInline.yxName,
        // bm_name: this.formInline.bmName,
        ispage: "1",
        page_no: this.currentPage,
        page_size: this.page_size,
      }).then((result) => {
        const { data } = result;
        const { list } = data;
        this.tableData = list;
        this.totalAll = data.count - 0;
        this.total_page = Math.ceil(this.totalAll / this.page_size);
        if (result.msg === "数据不存在") {
          this.$router.push({
            path: "/dataError",
          });
        } else if (result.msg === "项目已过期") {
          this.$router.push({
            path: "/overdueError",
          });
        }
        if (result.code === -1) {
          this.$router.push({
            path: "/rootError",
          });
        }
      });
    },
    // 全选按钮
    handleCheckAllChange() {
      if (this.$refs.multipleTable.selection.length < this.tableData.length) {
        this.checkAll = true;
      } else {
        this.checkAll = false;
      }
      this.$refs.multipleTable.toggleAllSelection();
    },
    backgroundShow() {
      this.$emit("backgroundShow", this.Showbg);
    },
  },
  created() {
    this.backgroundShow();
  },
  mounted() {
    const token = sessionStorage.getItem("token");
    this.token = token;
    this.queryUserReport();
  },
};
</script>
<style lang='scss' scoped>
.main {
  font-family: "Avenir", Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  padding: 0;
  color: #242748;
  margin: 0 auto;
  position: relative;
  font-size: 18px;
  margin: 0 50px 20px 30px;
  text-align: left;
  font-family: "PingFang";
  .container {
    width: 100%;
    height: 925px;
    padding-bottom: 30px;
    background-color: #fff;
    border-radius: 10px;
    .container-top {
      .top {
        display: flex;
        margin-bottom: 30px;
        .title {
          font-size: 26px;
          font-family: "黑体";
          color: #5673ff;
          margin-right: 60px;
        }
        .column {
          display: flex;
          font-size: 16px;
          .item {
            display: flex;
            align-items: center;
            padding-right: 20px;
            img {
              width: 15px;
              height: 15px;
              margin-right: 12px;
            }
          }
        }
      }
      .search {
        display: flex;
        align-items: center;
        & > div:first-child {
          display: flex;
          align-items: center;
          width: 35%;
          .del {
            position: relative;
            padding: 0 15px;
            margin: 0 25px;
            &::after {
              content: "";
              position: absolute;
              left: 0;
              top: 50%;
              transform: translateY(-50%);
              width: 1px;
              height: 80%;
              border-left: solid 1px #ccc;
            }
            &::before {
              content: "";
              position: absolute;
              right: 0;
              top: 50%;
              transform: translateY(-50%);
              width: 1px;
              height: 80%;
              border-right: solid 1px #ccc;
            }
          }
          /deep/.el-button {
            padding: 8px 12px;
          }
          .create {
            font-size: 14px;
          }
        }
        .selectCommon {
          width: 13%;
        }
        .number {
          width: 14%;
        }
        .name {
          width: 10%;
        }
        .state {
          width: 11%;
        }
        .principal {
          width: 15%;
        }
        /deep/.el-form {
          margin-left: 60px;
          width: 100%;
        }
        /deep/.el-checkbox {
          vertical-align: middle;
          /deep/.el-checkbox__label {
            font-size: 18px;
          }
          /deep/.el-checkbox__input.is-checked + .el-checkbox__label {
            color: #242748;
          }
        }
        .batchdelete {
          background-color: #e5eefe;
          border: solid 1px #c3e0fe;
          color: #2197fd;
        }
      }
      /deep/.el-button {
        padding: 11px 20px;
        border: none;
        font-family: "PingFang";
        font-weight: 700;
        color: #fff;
      }
      /deep/.el-badge__content.is-fixed.is-dot {
        left: 2px;
        top: 5px;
      }
      /deep/.el-form-item {
        margin-bottom: 0;
        font-size: 16px;
      }
      /deep/.el-form-item__content {
        line-height: 0;
      }
      /deep/input::placeholder {
        color: #2b3139;
        font-family: "PingFang";
      }
      /deep/.el-input__suffix {
        top: 50%;
        transform: translateY(-50%);
      }
      /deep/.el-input__inner {
        padding: 6px 15px;
        line-height: 0;
        height: auto;
        font-size: 16px;
        font-family: "PingFang";
      }
      /deep/.el-button--primary {
        background-color: #5672ff !important;
      }
      /deep/.el-input__icon {
        height: auto;
      }
      /deep/.el-select .el-input .el-select__caret {
        transform: none;
      }
      /deep/.el-icon-arrow-up:before {
        content: "\e615";
        font-family: "iconfont";
        font-size: 16px;
      }
    }
    .container-top {
      padding: 33px 45px 25px;
      border-bottom: solid 1px #e7e9eb;
    }
    tbody {
      color: #181b3b;
    }
    /deep/.el-table {
      .cell {
        font-size: 18px;
        button {
          font-size: 18px;
          font-family: "PingFang";
        }
        .edit,
        .del {
          color: #5673ff;
          padding-right: 15px;
          position: relative;
          &::before {
            content: "";
            position: absolute;
            right: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 1px;
            height: 15px;
            border-right: solid 1px #ccc;
          }
        }
        .del {
          color: #181b3b;
        }
        .disable {
          color: red;
        }
        .relieve {
          color: #03c5a9;
        }
      }
      /deep/.el-table__row {
        padding: 0 35px 0 45px;
      }
    }
    /deep/.el-table td,
    /deep/.el-table th {
      padding: 20px 0;
    }
    /deep/.el-table td:first-child,
    /deep/.el-table th:first-child {
      text-align: center;
      .cell {
        padding-right: 20px;
      }
    }
    /deep/.el-table td:nth-child(2) {
      .cell {
        margin-right: 15px;
      }
    }
    /deep/.el-table td:nth-child(2),
    /deep/.el-table td:nth-child(3) {
      padding: 0;
      .cell {
        padding: 0;
      }
    }
    /deep/tbody .el-table_1_column_10,
    /deep/tbody .el-table_1_column_11 {
      .cell {
        color: #5673ff;
      }
    }
    /deep/.el-checkbox__inner {
      width: 18px;
      height: 18px;
    }
    /deep/.el-checkbox__inner::after {
      width: 6px;
      height: 8px;
    }
    /deep/.el-checkbox__input.is-indeterminate .el-checkbox__inner::before {
      top: 50%;
      transform: translate(-50%, -50%);
      width: 8px;
      left: 50%;
    }
    /deep/.el-table--enable-row-hover .el-table__body tr:hover > td {
      background-color: #f0f5fb !important;
    }
    /deep/.el-checkbox__input.is-checked .el-checkbox__inner,
    /deep/.el-checkbox__input.is-indeterminate .el-checkbox__inner {
      background-color: #5672ff;
      border-color: #5672ff;
    }
    .block {
      position: absolute;
      bottom: 20px;
      left: 50%;
      transform: translateX(-50%);
      /deep/.el-input {
        width: 30px;
        height: 100%;
      }
      /deep/.el-input__inner {
        height: 25px;
        padding: 0;
        border: solid 1px #f0f1f4;
      }
      /deep/.el-pagination button,
      /deep/.el-pagination span,
      /deep/.el-pager li {
        height: 25px;
        line-height: 25px;
        color: #181b3b;
        font-weight: 700;
      }
      /deep/.el-pagination span {
        cursor: pointer;
      }
      /deep/.el-pagination.is-background .btn-next,
      /deep/.el-pagination.is-background .btn-prev,
      /deep/.el-pagination.is-background .el-pager li {
        background-color: #fff;
        border: solid 1px #f0f1f4;
        margin-right: 8px;
      }
      /deep/.el-pagination.is-background .el-pager li:not(.disabled).active {
        color: #fff;
        background-color: #5272fd;
        border: solid 1px #3957fd;
        font-size: 16px;
      }
      span {
        display: inline-block;
        width: 35px;
        height: 25px;
        line-height: 25px;
        border: solid 1px #f0f1f4;
        margin-left: 8px;
        border-radius: 2px;
      }
    }
    /deep/.el-pagination {
      text-align: center;
    }
    /deep/i.iconfont.icon-xiangmushenbao {
      margin-right: 5px;
    }
  }
  .green {
    background-color: #03c5a9;
  }
  .yellow {
    background-color: #f6ad3c;
    margin-left: 18px;
  }
}
</style>